<template>
	<div class="sys-container">
		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="媒体名称" prop="name"
							:rules="[{ required: true, message: '媒体名称不能为空', trigger: 'blur' }]">
							<el-input v-model.trim="state.ruleForm.name" placeholder="媒体名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="媒体简称" prop="name">
							<el-input v-model.trim="state.ruleForm.alias" placeholder="媒体简称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="媒体编码" prop="code"
							:rules="[{ required: true, message: '媒体编码不能为空', trigger: 'blur' }]">
							<el-input v-model.trim="state.ruleForm.code" placeholder="媒体编码" maxlength='6' clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="媒体平台">
							<el-input-number v-model.trim="state.ruleForm.flowPlatformEnum" :min='11' placeholder="媒体平台"
								class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态">
							<el-select v-model.trim="state.ruleForm.status" placeholder="请选择" class="w100">
								<el-option label="启用" :value="1"></el-option>
								<el-option label="停用" :value="2"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model.trim="state.ruleForm.remark" placeholder="请输入备注内容" clearable type="textarea"
								:autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="editDiMedia">
import { reactive, ref } from 'vue';
import mittBus from '/@/utils/mitt';

import { DiMedia } from '/@/api/models';
import SysDiMediaApi from '/@/api/delivery/media';
import commonFunction from '/@/utils/commonFunction';
import { ElMessage } from 'element-plus';

// 定义变量内容
const { generateRandomLowercaseString } = commonFunction();
const props = defineProps({
	title: String,
});

const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as DiMedia,
});

// 打开弹窗
const openDialog = (row: DiMedia, ages: string) => {
	if (ages === 'add') {
		state.ruleForm = row;
		state.isShowDialog = true;
		state.ruleForm.code = generateRandomLowercaseString(6);
	} else {
		state.ruleForm = row;
		state.isShowDialog = true;
	}
};

// 关闭弹窗
const closeDialog = () => {
	mittBus.emit('submitRefresh');
	state.isShowDialog = false;
};	

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		let res;
		if (state.ruleForm.id != undefined && state.ruleForm.id > 0) {
			res = await SysDiMediaApi().editPost(state.ruleForm);
		} else {
			res = await SysDiMediaApi().addPost(state.ruleForm);
		}
		if (res.data.resCode != '0000')
			ElMessage.error(res.data.resMessage);
		else {
			ElMessage.success("操作成功");
			closeDialog();
		}
	});
};

// 导出对象
defineExpose({ openDialog });
</script>
